﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using TccServicos.Models;

namespace TccServicos.Util
{
    public static class Util
    {
        #region Metodos de Ajuda WebApi
        public static RetornoServico RetornoErro(string pMensagem)
        {
            return new RetornoServico()
            {
                status = 0,
                mensagem = pMensagem,
            };
        }

        public static RetornoServico RetornoOk(int idInclusao = 0)
        {
            return new RetornoServico()
            {
                status = 1,
                id = idInclusao,
            };
        }
        #endregion

        #region Metodos Uteis
        public static string NextHex(int digits)
        {
            Random random = new Random();

            byte[] buffer = new byte[digits / 2];

            random.NextBytes(buffer);

            string result = String.Concat(buffer.Select(x => x.ToString("X2")).ToArray());

            if (digits % 2 == 0)
                return result;

            result += random.Next(16).ToString("X");

            return result;
        }

        public static string NextIntCadastro()
        {
            Random random = new Random();

            string result = "";
            for (int i = 0; i < 6; i++)
            {
                result += random.Next(9).ToString();    
            }

            return result;
        }

        public static string BuscaConfig(string config)
        {
            int i = 0;
            foreach (var item in ConfigurationManager.AppSettings.AllKeys)
            {
                if (item == config)
                {
                    return ConfigurationManager.AppSettings[i];
                }
                i++;
            }
            return "";
        }
        #endregion
    }
}